using System;
using System.Timers;

namespace RxjhServer
{
	public class EveFCClass : IDisposable
	{
		private System.Timers.Timer 时间1;

		private System.Timers.Timer 时间2;

		private System.Timers.Timer 时间3;

		private System.Timers.Timer 时间4;

		private System.Timers.Timer 时间5;

		private DateTime kssj;

		private DateTime kssjgj;

		private int kssjint;

		public Players PlayerA;

		public Players PlayerB;

		public string PVPName = string.Empty;

		private int PVPType;

		private int MapID;

		public ThreadSafeDictionary<int, Players> playlist = new ThreadSafeDictionary<int, Players>();

		public EveFCClass(ThreadSafeDictionary<int, Players> players, int type)
		{
			try
			{
				kssj = DateTime.Now.AddMinutes(1.0);
				PVPType = type;
				foreach (Players value in players.Values)
				{
					playlist.Add(value.人物全服ID, value);
					if (PlayerA == null)
					{
						PlayerA = value;
						PlayerA.FC分数 = 0;
						PlayerA.系统提示("复仇期间不能使用回城符、土灵符、追杀和传送命令等功能!", 10, "复仇规则");
						PlayerA.系统提示("超出范围扣除一分!达到负10分则为输!复仇起间下线也算输!", 10, "复仇规则");
						PlayerA.系统提示("此地图死亡掉落5000武勋,对方获得5000武勋!", 10, "复仇规则");
						PlayerA.系统提示("反之谁先获得10分谁获胜!杀死对方10次!", 10, "复仇规则");
						PlayerA.系统提示("获胜者获得对方5W武勋!失败掉5W武勋!", 10, "复仇规则");
						PlayerA.系统提示("失败惩罚减少打怪获得经验" + (double.Parse(World.fc[6]) * 100.0).ToString() + "%", 10, "复仇规则");
					}
					else if (PlayerB == null)
					{
						PlayerB = value;
						PlayerB.FC分数 = 0;
						PlayerB.系统提示("复仇期间不能使用回城符、土灵符、追杀和传送命令等功能!", 10, "复仇规则");
						PlayerB.系统提示("超出范围扣除一分!达到负10分则为输!复仇起间下线也算输!", 10, "复仇规则");
						PlayerB.系统提示("此地图死亡掉落5000武勋,对方获得5000武勋!", 10, "复仇规则");
						PlayerB.系统提示("反之谁先获得10分谁获胜!杀死对方10次!", 10, "复仇规则");
						PlayerB.系统提示("获胜者获得对方5W武勋!失败掉5W武勋!", 10, "复仇规则");
						PlayerB.系统提示("失败惩罚减少打怪获得经验" + (double.Parse(World.fc[6]) * 100.0).ToString() + "%", 10, "复仇规则");
					}
				}
				MapID = int.Parse(World.fc[0]);
				PVPName = "复仇区";
				World.复仇进程 = 2;
				时间1 = new System.Timers.Timer(60000.0);
				时间1.Elapsed += 时间结束事件1;
				时间1.Enabled = true;
				时间1.AutoReset = true;
				时间结束事件1(null, null);
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "FC EveClass 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件1(object source, ElapsedEventArgs e)
		{
			try
			{
				int num = kssjint = (int)kssj.Subtract(DateTime.Now).TotalSeconds;
				foreach (Players value in World.allConnectedChars.Values)
				{
					value.系统提示("[" + PlayerA.UserName + "] 和 [" + PlayerB.UserName + "]即将在" + PVPName + "场复仇," + kssjint.ToString() + "秒后计分。", 19, PVPName);
				}
				if (kssjint <= 0)
				{
					foreach (Players value2 in playlist.Values)
					{
						if (!value2.检查玩家是否在复仇区内(value2))
						{
							value2.移动(float.Parse(World.fc[1]), float.Parse(World.fc[2]), 15f, int.Parse(World.fc[0]));
						}
						value2.系统公告("开始计分...");
					}
					时间1.Enabled = false;
					时间1.Close();
					时间1.Dispose();
					World.复仇进程 = 3;
					kssjgj = DateTime.Now.AddMinutes(10.0);
					时间2 = new System.Timers.Timer(10000.0);
					时间2.Elapsed += 时间结束事件2;
					时间2.Enabled = true;
					时间2.AutoReset = true;
					时间5 = new System.Timers.Timer(3000.0);
					时间5.Elapsed += 时间结束事件5;
					时间5.Enabled = true;
					时间5.AutoReset = true;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "PVP 时间结束事件1 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件2(object source, ElapsedEventArgs e)
		{
			try
			{
				int num = (int)kssjgj.Subtract(DateTime.Now).TotalSeconds;
				foreach (Players value in playlist.Values)
				{
					value.系统提示(PlayerA.UserName + "->[" + PlayerA.FC分数.ToString() + "] VS " + PlayerB.UserName + "->[" + PlayerB.FC分数.ToString() + "] 复仇结束倒计时" + num.ToString() + "秒。", 19, PVPName);
				}
				if (num <= 0)
				{
					World.复仇进程 = 6;
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
					时间5.Enabled = false;
					时间5.Close();
					时间5.Dispose();
					时间3 = new System.Timers.Timer(10000.0);
					时间3.Elapsed += 时间结束事件3;
					时间3.Enabled = true;
					时间3.AutoReset = false;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "PVP 时间结束事件2 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件3(object source, ElapsedEventArgs e)
		{
			try
			{
				if (!PlayerA.Client.Running && !PlayerB.Client.Running)
				{
					foreach (Players value in World.allConnectedChars.Values)
					{
						value.系统提示("复仇时间到！由于双方中场退出,复仇比赛结束。", 19, PVPName);
					}
					Dispose();
				}
				else if (!PlayerA.Client.Running)
				{
					foreach (Players value2 in World.allConnectedChars.Values)
					{
						value2.系统提示("复仇时间到！由于[" + PlayerA.UserName + "]中场退出,[" + PlayerB.UserName + "]获胜,复仇比赛结束。", 19, PVPName);
					}
					Dispose();
				}
				else if (!PlayerB.Client.Running)
				{
					foreach (Players value3 in World.allConnectedChars.Values)
					{
						value3.系统提示("复仇时间到！由于[" + PlayerB.UserName + "]中场退出,[" + PlayerA.UserName + "]获胜,复仇比赛结束。", 19, PVPName);
					}
					Dispose();
				}
				else if (PlayerA.FC分数 == PlayerB.FC分数)
				{
					foreach (Players value4 in World.allConnectedChars.Values)
					{
						value4.系统提示("复仇时间到,双方没有在时间内分出胜负,比分相等.均不受到惩罚。", 19, PVPName);
					}
					Dispose();
				}
				else if (PlayerA.FC分数 > PlayerB.FC分数)
				{
					foreach (Players value5 in World.allConnectedChars.Values)
					{
						value5.系统提示("复仇时间到,[" + PlayerA.UserName + "][" + PlayerA.FC分数.ToString() + "] VS [" + PlayerB.UserName + "][" + PlayerB.FC分数.ToString() + "] 。", 19, PVPName);
						value5.系统提示("[" + PlayerA.UserName + "]获胜,复仇比赛结束。", 19, PVPName);
					}
					int num = 60000 * int.Parse(World.fc[5]);
					if (PlayerB.追加状态列表.ContainsKey(int.Parse(World.fc[4])))
					{
						追加状态类 追加状态类 = PlayerB.追加状态列表[int.Parse(World.fc[4])];
						追加状态类.时间结束事件();
					}
					if (PlayerB.追加状态列表.ContainsKey(int.Parse(World.dt[4])))
					{
						追加状态类 追加状态类2 = PlayerB.追加状态列表[int.Parse(World.dt[4])];
						追加状态类2.时间结束事件();
					}
					追加状态类 追加状态类3 = new 追加状态类(PlayerB, num, int.Parse(World.fc[4]), 1);
					PlayerB.追加状态列表.Add(追加状态类3.FLD_PID, 追加状态类3);
					PlayerB.FLD_人物_复仇_经验百分比 = double.Parse(World.fc[6]);
					PlayerB.状态效果(BitConverter.GetBytes(int.Parse(World.fc[4])), 1, num);
					bool flag = false;
					if (PlayerB.Player_WuXun >= 50000)
					{
						flag = true;
						PlayerB.Player_WuXun -= 50000;
						PlayerA.Player_WuXun += 50000;
						PlayerA.更新武功和状态();
						PlayerA.系统提示("获胜获得5W武勋, 复仇成功。", 19, PVPName);
						PlayerB.系统提示("战败获得惩罚打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %减少5W武勋, 复仇比赛结束。", 19, PVPName);
					}
					PlayerB.复仇更新人物减经验();
					PlayerB.更新武功和状态();
					if (!flag)
					{
						PlayerB.系统提示("战败获得惩罚打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %, 复仇比赛结束。", 19, PVPName);
					}
					Dispose();
				}
				else if (PlayerA.FC分数 < PlayerB.FC分数)
				{
					foreach (Players value6 in World.allConnectedChars.Values)
					{
						value6.系统提示("复仇时间到,[" + PlayerA.UserName + "][" + PlayerA.FC分数.ToString() + "] VS [" + PlayerB.UserName + "][" + PlayerB.FC分数.ToString() + "] 。", 19, PVPName);
						value6.系统提示("[" + PlayerB.UserName + "]获胜,复仇比赛结束。", 19, PVPName);
					}
					int num2 = 60000 * int.Parse(World.fc[5]);
					if (PlayerA.追加状态列表.ContainsKey(int.Parse(World.fc[4])))
					{
						追加状态类 追加状态类4 = PlayerA.追加状态列表[int.Parse(World.fc[4])];
						追加状态类4.时间结束事件();
					}
					if (PlayerA.追加状态列表.ContainsKey(int.Parse(World.dt[4])))
					{
						追加状态类 追加状态类5 = PlayerA.追加状态列表[int.Parse(World.dt[4])];
						追加状态类5.时间结束事件();
					}
					追加状态类 追加状态类6 = new 追加状态类(PlayerA, num2, int.Parse(World.fc[4]), 1);
					PlayerA.追加状态列表.Add(追加状态类6.FLD_PID, 追加状态类6);
					PlayerA.FLD_人物_复仇_经验百分比 = double.Parse(World.fc[6]);
					PlayerA.状态效果(BitConverter.GetBytes(int.Parse(World.fc[4])), 1, num2);
					PlayerA.复仇更新人物减经验();
					bool flag2 = false;
					if (PlayerA.Player_WuXun >= 50000)
					{
						flag2 = true;
						PlayerA.Player_WuXun -= 50000;
						PlayerB.Player_WuXun += 50000;
						PlayerB.更新武功和状态();
						PlayerB.系统提示("获胜获得5W武勋, 复仇成功。", 19, PVPName);
						PlayerA.系统提示("战败获得惩罚打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %减少5W武勋, 复仇比赛结束。", 19, PVPName);
					}
					PlayerA.更新武功和状态();
					if (!flag2)
					{
						PlayerA.系统提示("战败获得惩罚打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %, 复仇比赛结束。", 19, PVPName);
					}
					Dispose();
				}
				World.复仇进程 = 5;
				kssjgj = DateTime.Now.AddMinutes(1.0);
				时间3.Enabled = false;
				时间3.Close();
				时间3.Dispose();
				时间4 = new System.Timers.Timer(30000.0);
				时间4.Elapsed += 时间结束事件4;
				时间4.Enabled = true;
				时间4.AutoReset = true;
				时间5.Enabled = false;
				时间5.Close();
				时间5.Dispose();
			}
			catch (Exception ex)
			{
				Dispose();
				Form1.WriteLine(1, "PVP 时间结束事件3 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件4(object source, ElapsedEventArgs e)
		{
			try
			{
				int num = (int)kssjgj.Subtract(DateTime.Now).TotalSeconds;
				if (num <= 0)
				{
					时间4.Enabled = false;
					时间4.Close();
					时间4.Dispose();
					World.复仇进程 = 4;
					Dispose();
				}
			}
			catch (Exception ex)
			{
				Dispose();
				Form1.WriteLine(1, "PVP 时间结束事件4 出错：" + ex?.ToString());
			}
		}

		public void 时间结束事件5(object source, ElapsedEventArgs e)
		{
			try
			{
				foreach (Players value in playlist.Values)
				{
					if (value.人物PK模式 != 2)
					{
						value.切换PK模式(2);
					}
					if (!value.检查玩家是否在复仇区内(value))
					{
						value.移动(float.Parse(World.fc[1]), float.Parse(World.fc[2]), 15f, int.Parse(World.fc[0]));
						value.FC分数--;
						value.系统提示("你已累计逃跑一次,扣除一分。累计负10分后,视为输!", 19, "系統警告");
						break;
					}
					if (value.FC分数 <= -5)
					{
						if (value == PlayerA && PlayerA != null)
						{
							int num = 60000 * int.Parse(World.fc[5]);
							if (PlayerA.追加状态列表.ContainsKey(int.Parse(World.fc[4])))
							{
								追加状态类 追加状态类 = PlayerA.追加状态列表[int.Parse(World.fc[4])];
								追加状态类.时间结束事件();
							}
							if (PlayerA.追加状态列表.ContainsKey(int.Parse(World.dt[4])))
							{
								追加状态类 追加状态类2 = PlayerA.追加状态列表[int.Parse(World.dt[4])];
								追加状态类2.时间结束事件();
							}
							追加状态类 追加状态类3 = new 追加状态类(PlayerA, num, int.Parse(World.fc[4]), 1);
							PlayerA.追加状态列表.Add(追加状态类3.FLD_PID, 追加状态类3);
							PlayerA.FLD_人物_复仇_经验百分比 = double.Parse(World.fc[6]);
							PlayerA.状态效果(BitConverter.GetBytes(int.Parse(World.fc[4])), 1, num);
							bool flag = false;
							if (PlayerA.Player_WuXun >= 50000)
							{
								flag = true;
								PlayerA.Player_WuXun -= 50000;
								PlayerB.Player_WuXun += 50000;
								PlayerB.更新武功和状态();
								PlayerB.系统提示("获胜获得5W武勋, 复仇成功。", 19, PVPName);
								PlayerA.系统提示("累计逃跑次数10次,打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %减少5w武勋, 复仇失败。", 19, PVPName);
							}
							PlayerA.复仇更新人物减经验();
							PlayerA.更新武功和状态();
							if (!flag)
							{
								PlayerA.系统提示("累计逃跑次数10次,打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %, 复仇失败。", 19, PVPName);
							}
							World.conn.发送("全服公告|6|系统提示|复仇结束[" + PlayerB.UserName + "]获胜,复仇比赛结束。");
							Dispose();
						}
						else if (value == PlayerB && PlayerB != null)
						{
							int num2 = 60000 * int.Parse(World.fc[5]);
							if (PlayerB.追加状态列表.ContainsKey(int.Parse(World.fc[4])))
							{
								追加状态类 追加状态类4 = PlayerB.追加状态列表[int.Parse(World.fc[4])];
								追加状态类4.时间结束事件();
							}
							if (PlayerB.追加状态列表.ContainsKey(int.Parse(World.dt[4])))
							{
								追加状态类 追加状态类5 = PlayerB.追加状态列表[int.Parse(World.dt[4])];
								追加状态类5.时间结束事件();
							}
							追加状态类 追加状态类6 = new 追加状态类(PlayerB, num2, int.Parse(World.fc[4]), 1);
							PlayerB.追加状态列表.Add(追加状态类6.FLD_PID, 追加状态类6);
							PlayerB.FLD_人物_复仇_经验百分比 = double.Parse(World.fc[6]);
							PlayerB.状态效果(BitConverter.GetBytes(int.Parse(World.fc[4])), 1, num2);
							bool flag2 = false;
							if (PlayerB.Player_WuXun >= 50000)
							{
								flag2 = true;
								PlayerB.Player_WuXun -= 50000;
								PlayerA.Player_WuXun += 50000;
								PlayerA.更新武功和状态();
								PlayerA.系统提示("获胜获得5W武勋, 复仇成功。", 19, PVPName);
								PlayerB.系统提示("累计逃跑次数10次,打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %减少5W武勋, 复仇失败。", 19, PVPName);
							}
							PlayerB.复仇更新人物减经验();
							PlayerB.更新武功和状态();
							if (!flag2)
							{
								PlayerB.系统提示("累计逃跑次数10次,打怪获得经验降低" + (double.Parse(World.fc[6]) * 100.0).ToString() + " %, 复仇失败。", 19, PVPName);
							}
							World.conn.发送("全服公告|6|系统提示|复仇结束[" + PlayerA.UserName + "]获胜,复仇比赛结束。");
							Dispose();
						}
						break;
					}
				}
			}
			catch (Exception ex)
			{
				Dispose();
				Form1.WriteLine(1, "PVP 时间结束事件6 出错：" + ex?.ToString());
			}
		}

		public void Dispose()
		{
			try
			{
				if (时间1 != null)
				{
					时间1.Enabled = false;
					时间1.Close();
					时间1.Dispose();
				}
				if (时间2 != null)
				{
					时间2.Enabled = false;
					时间2.Close();
					时间2.Dispose();
				}
				if (时间3 != null)
				{
					时间3.Enabled = false;
					时间3.Close();
					时间3.Dispose();
				}
				if (时间4 != null)
				{
					时间4.Enabled = false;
					时间4.Close();
					时间4.Dispose();
				}
				if (时间5 != null)
				{
					时间5.Enabled = false;
					时间5.Close();
					时间5.Dispose();
				}
				foreach (Players value in playlist.Values)
				{
					value.FC分数 = 0;
					value.移动(529f, 1528f, 15f, 101);
					value.切换PK模式(0);
				}
				PlayerA = null;
				PlayerB = null;
				playlist.Clear();
				World.eve80Players.Clear();
			}
			catch
			{
				PlayerA = null;
				PlayerB = null;
				World.eve80Players.Clear();
				playlist.Clear();
			}
			finally
			{
				PlayerA = null;
				PlayerB = null;
				World.eve80Players.Clear();
				playlist.Clear();
			}
			World.复仇 = null;
			World.复仇进程 = 0;
		}
	}
}
